package com.example.ll_video.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.ll_video.bean.PayType;
import com.example.ll_video.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class PayTypeAdapter extends RecyclerView.Adapter<PayTypeAdapter.ViewHolder> {



    private Context context;
    private ItemClickListener itemClickListener;
    private List<PayType> myData;
    private int selectedPosition = 0;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public PayTypeAdapter(Context context, List<PayType> myData) {
        this.context = context;
        this.myData = myData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.pay_type_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PayType payType = myData.get(position);
        if (payType.isSelected()) {
            holder.ivSelect.setImageResource(R.mipmap.abc_btn_radio_to_on_mtrl_015);
        } else {
            holder.ivSelect.setImageResource(R.mipmap.abc_btn_radio_to_on_mtrl_000);
        }
        holder.tvPayName.setText(payType.getMemo());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position, payType);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_select)
        ImageView ivSelect;
        @BindView(R.id.iv_type_icon)
        ImageView ivTypeIcon;
        @BindView(R.id.tv_pay_name)
        TextView tvPayName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface ItemClickListener {
        void onItemClick(int position, PayType payType);
    }

    public void refresh(int position) {
        if (position != selectedPosition) {
            PayType payType1 = myData.get(selectedPosition);
            payType1.setSelected(false);
            notifyItemChanged(selectedPosition, payType1);

            PayType payType2 = myData.get(position);
            payType2.setSelected(true);
            notifyItemChanged(position, payType2);

            selectedPosition = position;
        }
    }
}
